package com.beyondsoft.sabg.edu.repository.core;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.NoRepositoryBean;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

/**
 * @Author Mo cr
 * @Date 2023/8/4
 */
@NoRepositoryBean
public interface SimpleRepository<T> extends JpaRepository<T, Long> {
    List<T> findAllByValidity(Boolean b);

    Page<T> findAllByValidity(Pageable p, Boolean b);

    Page<T> findAll(Specification<T> s, Pageable p);

    Optional<T> findByIdAndValidity(Long id, Boolean b);

    List<T> findAllByIdInAndValidity(Iterable<Long> ids, Boolean b);

    Page<T> findAllByIdInAndValidity(Iterable<Long> ids, Boolean b, Pageable p);

    @Query(value = "update #{#entityName} set validity=false where id in ?1 ")
    @Transactional
    @Modifying
    void deleteInBatch(List<Long> ids);
}
